package com.kaibai.controller;

import com.kaibai.pojo.Cart;
import com.kaibai.pojo.CartItem;
import com.kaibai.pojo.CartItemDB;
import com.kaibai.pojo.Good;
import com.kaibai.service.CartService;
import com.kaibai.service.GoodService;
import com.kaibai.service.impl.CartServiceImpl;
import com.kaibai.service.impl.GoodServiceImpl;
import com.kaibai.utils.PrintJson;
import com.kaibai.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class CartServlet extends BaseServlet {
    private GoodService service = new GoodServiceImpl();
    private CartService cartService = new CartServiceImpl();
    /**
     * 加入商品到购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws  IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        Good good = service.getGoodById(id);
        CartItem cartItem = new CartItem(good.getId(),good.getName(),1,good.getPrice(),good.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if(cart == null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void addItemDB(HttpServletRequest request, HttpServletResponse response) throws  IOException {
        int gid = WebUtil.parseInt(request.getParameter("gid"),0);
        int uid = WebUtil.parseInt(request.getParameter("uid"),0);
        Good good = service.getGoodById(gid);
        int count = 1;
        CartItemDB cartItemDB = cartService.existItemById(gid,uid);
        if(cartItemDB==null){
            CartItemDB cartItemDB1 = new CartItemDB(null,uid,gid,good.getName(),count,good.getPrice());
            cartService.addCartItemDB(cartItemDB1);
        }else{
            count += cartItemDB.getCount();
            cartService.updateItemDB(gid,count,uid);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    /**
     *删除商品项
     * @param request
     * @param response
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            cart.deleteItem(id);
            //回到原来的页面
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    /**
     * 清空购物车
     * @param request
     * @param response
     * @throws IOException
     */
    protected void clearCart(HttpServletRequest request, HttpServletResponse response) throws  IOException{
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            cart.clear();
            //回到原来的页面
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    /**
     * 修改购物车商品数量
     * @param request
     * @param response
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws  IOException{
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        int count = WebUtil.parseInt(request.getParameter("count"),1);
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            cart.updateCount(id,count);
            //回到原来的页面
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    protected  void List(HttpServletRequest request, HttpServletResponse response){
        int uid = WebUtil.parseInt(request.getParameter("uId"),0);
        List<CartItemDB> list = cartService.getListByUid(uid);
        PrintJson.printJsonObj(response,list);
    }
    protected  void updateItemDB(HttpServletRequest request, HttpServletResponse response){
        int gid = WebUtil.parseInt(request.getParameter("gId"),0);
        int count = WebUtil.parseInt(request.getParameter("count"),1);
        int uid = WebUtil.parseInt(request.getParameter("uId"),0);
        boolean flag = cartService.updateItemDB(gid,count,uid);
        PrintJson.printJsonFlag(response,flag);
    }

    protected void deleteItemDB(HttpServletRequest request,HttpServletResponse response) throws IOException{
        int gid = WebUtil.parseInt(request.getParameter("gId"),0);
        int uid = WebUtil.parseInt(request.getParameter("uId"),0);
        boolean flag = cartService.deleteItemDB(gid,uid);
        PrintJson.printJsonFlag(response,flag);
    }

    protected void deleteItemDBByUid(HttpServletRequest request,HttpServletResponse response) throws IOException{
        int uid = WebUtil.parseInt(request.getParameter("uId"),0);
        boolean flag = cartService.deleteItemDBByUid(uid);
        PrintJson.printJsonFlag(response,flag);
    }

}
